package com.hundred.data.check;

import java.io.IOException;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hundred.item.dao.ItemDao;
import com.hundred.item.dao.dataobject.ItemDO;

public class ItemOnlineInfoChecker {

		private static final Logger logger = LoggerFactory.getLogger(ItemOnlineInfoChecker.class);
		@Autowired
		private ItemDao itemDao;

		public void process(){
			
			List<ItemDO> itemList = itemDao.getInitCacheItems(0);
			
			for(ItemDO itemDO:itemList){
				
				String itemUrl = itemDO.getItemUrl();
				logger.error("Processing URL:"+itemUrl);
				HttpClient client = new HttpClient(); 
			     
				HttpMethod method=new GetMethod(itemUrl);
			      
			    try {
					client.executeMethod(method);
				} catch (HttpException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				} catch (IOException e) {
					//e.printStackTrace();
				}

			    //打印服务器返回的状态
			    //System.out.println(method.getStatusLine());
			    //打印返回的信息
			    String content=null;
				try {
					
					Thread.sleep(1000);
					
					content = method.getResponseBodyAsString();
				    if(content!=null && content.indexOf("此宝贝已下架")>-1){
				    	logger.error("宝贝已下架:"+itemUrl);
				    	itemDO.setItemStatus(2);
				    	itemDao.updateItem(itemDO);
				    }
				    if(content!=null && content.indexOf("此商品已下架")>-1){
				    	logger.error("宝贝已下架:"+itemUrl);
				    	itemDO.setItemStatus(2);
				    	itemDao.updateItem(itemDO);
				    }
				    if(content!=null && content.indexOf("可能已下架或者被转移")>-1){
				    	logger.error("宝贝可能已下架或者被转移:"+itemUrl);
				    	itemDO.setItemStatus(2);
				    	itemDao.updateItem(itemDO);
				    }
				} catch (Exception e) {
					logger.error(e.toString());
				}
			    
			}
			

		}
		public ItemDao getItemDao() {
			return itemDao;
		}

		public void setItemDao(ItemDao itemDao) {
			this.itemDao = itemDao;
		}

}
